/*
*
* Copyright (c) 2011 Vaulting Systems International
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy 
* of this software and associated documentation files (the "Software"), to deal 
* in the Software without restriction, including without limitation the rights 
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
* of the Software, and to permit persons to whom the Software is furnished to do  
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all  
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE  
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package com.ekeyman.client.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import com.ekeyman.client.service.EkeymanService;
import com.ekeymanlib.dto.AppDeviceApiKeys;
import com.ekeymanlib.dto.EncryptionKeys;

public class LoadDataForjQueryGridUT {
	
	private static final String[] SPRING_CONFIG_FILES = new String[]{"applicationContext.xml"};
	private EkeymanService ekeymanService;
	ClassPathXmlApplicationContext ctx;
	

	@Parameters({"openidurl","emailAddress"}) 
	@Test
	public void testLoad(
			@Optional("http://JB2008NC.myopenid.com/") String openidurl,
			@Optional("james.bell51@estateplusplus.com") String emailAddress) {

		System.out.println("Entered testLoad...");
		String apiKey = getEkeymanService().registerVendor(
						"Jimmy Bell", 
						"4339 Castleton Road", 
						"Charlotte", 
						"NC", 
						"28211", 
						"704-942-7257", 
						emailAddress);
		getEkeymanService().registerUser(apiKey, openidurl);

		int secretKeyLength = 16; // KeyGenerator algorithm-specific metric specified in number of bytes. 
		int ivLength = 8; // in bytes
		int saltLength = 8; // in bytes
		int iterationCountMax = 1024;

		String application = "BillPayApp";
		
		for(int i = 0; i < 30; i++){
			
			String device = "704-555-1212x" + Integer.toString(i);
			AppDeviceApiKeys appDeviceApiKeys = getEkeymanService().registerAppDevice(
					apiKey, application, device);
			
			for(int j = 0; j < 30; j++){
				String location = Integer.toString(i) + "," + Integer.toString(j);
				String resourceUri = "/billpayapp/transactions/TRN" + Integer.toString(j) + ".txt";
		    	EncryptionKeys keysToEncrypt = getEkeymanService().createEncryptionKeys(resourceUri, 
		    			appDeviceApiKeys.getPublicKey(), appDeviceApiKeys.getPrivateKey(), 
		    			secretKeyLength, ivLength, saltLength, iterationCountMax, location);
			}
		}
		
	    System.out.println("Exited testLoad...");
	}
	
	@BeforeSuite
	public void beforeSuite() {
		System.out.println("Before suite...");
		
		ctx = new ClassPathXmlApplicationContext(SPRING_CONFIG_FILES);
		
		setEkeymanService((EkeymanService)ctx.getBeanFactory().getBean("serviceClient"));
	}
	
	@AfterSuite
	public void afterSuite() {
		System.out.println("After suite...");
	}
	
	public void setEkeymanService(EkeymanService ekeymanService) {
		this.ekeymanService = ekeymanService;
	}
	public EkeymanService getEkeymanService() {
		return ekeymanService;
	}
}
